package getBaiduPaid;

import java.io.IOException;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class PaidListingFetcher {
	public static String[] CONTAINERL = {"table.ec_pp_f","div.ec_pp_f"};
	public static String[] CONTAINERR = {"div.ec_im"};
	
	public static String[] LISTINGTITLE = {"a.ec_favicon","a.ec_title","a.EC_title","a.EC_t"};
	public static String[] LISTINGDESC = {"a.ec_favicon","a.ec_desc","a.EC_desc"};
	public static String[] LISTINGURL = {"*.ec_url","*.EC_url"};
	
	public Document getSERP (String url) throws IOException {
		JsoupParse parser = new JsoupParse();
		return parser.ParseURL(url);
	}
	
	//根据class属性值来获取代码块
	public Elements getContainersLeft (Document doc) throws IOException {
		for (String s : CONTAINERL) {
			if (doc.select(s).hasText()) return doc.select(s);
		}
		return doc.select("span.nums");
	}
	
	//获取右边展示的Container元素
	public Elements getContainerRight (Document doc) throws IOException {
		for (String s : CONTAINERR) {
			if (doc.select(s).hasText()) return doc.select(s);
		}
		return doc.select("span.nums");
	}
		
	public Element getListingTitle (Element el) throws IOException {
		for (String s : LISTINGTITLE) {
			if (el.select(s).hasText()) return el.select(s).first();
		}
		return el;
	}
	
	public Element getListingDesc (Element el) throws IOException {
		for (String s : LISTINGDESC) {
			if (el.select(s).hasText()) return el.select(s).first();
		}
		return el;
	}
	
	public Element getListingURL (Element el) throws IOException {
		for (String s : LISTINGURL) {
			if (el.select(s).hasText()) return el.select(s).first();
		}
		return el;
	}
	
	//选择百度左边或者右边排名
	public Elements RankingPositioner (Document doc,String leftORright) throws IOException {
		if (leftORright.equals("right") || leftORright.equals("Right")) return getContainerRight(doc);
		else if (leftORright.equals("left") || leftORright.equals("Left")) return getContainersLeft(doc);
		else return doc.select("span.nums");
	}
}